package com.cqeec.util;

import java.text.DecimalFormat;
import java.util.Calendar;

/**
 * 
 * @author HuaL
 * @description 数据类型检查
 */
public class ValidateUtil {
	/**
	 * 
	 * @param number
	 * @return 能正常转换返回带两位数字的小数字符串，否则返回err
	 */
	public static String StringTodouble(String number) {
		Double d = new Double(-1.00);
		DecimalFormat df = new DecimalFormat("#.00");
		try {
			d = new Double(number);
			return df.format(d);
		} catch (Exception e) {
			return new String("err");
		}
	}
	/**
	 * 
	 * @param date 格式严格按照yyyy-MM-dd，例如2016-09-10
	 * @return 转换成功返回true，否则返回false
	 */
	public static boolean StringToDate(String date) {
		boolean ret = true;
		try {
			int year = new Integer(date.substring(0, 4)).intValue();
			int month = new Integer(date.substring(5, 7)).intValue();
			int day = new Integer(date.substring(8)).intValue();
			Calendar cal = Calendar.getInstance();
			cal.setLenient(false); // 允许严格检查日期格式
			cal.set(year, month - 1, day);
			cal.getTime();
		} catch (Exception e) {
			ret = false;
		}
		return ret;
	}
	/**
     * 
     * @param number  字符串数字
     * @return     转换成功返回true，否则返回false
     */
    public static boolean StringToInt(String number) {
    	try {
    		Integer.valueOf(number);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
    }
}
